/*
   Copyright 2011-2012 gtalent2@gmail.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package messagepane

import (
	"github.com/gtalent/starfish/gfx"
	"github.com/gtalent/starfish/input"
	"github.com/gtalent/starfish/util"
)

type FourButtonMenu struct {
	size    util.Size
	pressed [4]bool
	buttons [4]util.Bounds
	text    [4]gfx.Text
	//listens for a button press, and passes in an int indicating the button
	//0: top left, 1: top right, 2: bottom left, 3: bottom right
	selectionListener func(int)
}

func NewFourButtonMenu(names []string, font *gfx.Font, listener func(int)) *FourButtonMenu {
	fbm := new(FourButtonMenu)
	fbm.buttons[0] = util.Bounds{util.Point{0, 0}, fbm.size.DivideOf(util.Size{2, 2})}
	fbm.buttons[1] = util.Bounds{util.Point{fbm.size.Width >> 1, 0}, fbm.size.DivideOf(util.Size{2, 2})}
	fbm.buttons[2] = util.Bounds{util.Point{0, fbm.size.Height >> 1}, fbm.size.DivideOf(util.Size{2, 2})}
	fbm.buttons[3] = util.Bounds{util.Point{fbm.size.Width >> 1, fbm.size.Height >> 1}, fbm.size.DivideOf(util.Size{2, 2})}

	fbm.selectionListener = listener
	font.SetColor(gfx.Color{0, 0, 0, 255})
	for i, v := range names {
		font.WriteTo(v, &fbm.text[i])
	}
	return fbm
}

func (me *FourButtonMenu) SetSize(size util.Size) {
	me.size = size
	me.buttons[0] = util.Bounds{util.Point{0, 0}, me.size.DivideOf(util.Size{2, 2})}
	me.buttons[1] = util.Bounds{util.Point{me.size.Width >> 1, 0}, me.size.DivideOf(util.Size{2, 2})}
	me.buttons[2] = util.Bounds{util.Point{0, me.size.Height >> 1}, me.size.DivideOf(util.Size{2, 2})}
	me.buttons[3] = util.Bounds{util.Point{me.size.Width >> 1, me.size.Height >> 1}, me.size.DivideOf(util.Size{2, 2})}
}

func (me *FourButtonMenu) Free() {
	for i := 0; i < len(me.text); i++ {
		me.text[i].Free()
	}
}

func (me *FourButtonMenu) MouseButtonPress(e input.MouseEvent) {
	//clear previous presses, that for some reason did not receive a release
	for i, _ := range me.pressed {
		me.pressed[i] = false
	}
	for i, v := range me.buttons {
		if v.ContainsPoint(e.Point) {
			me.pressed[i] = true
			break
		}
	}
}

func (me *FourButtonMenu) MouseButtonRelease(e input.MouseEvent) {
	for i, v := range me.buttons {
		if v.ContainsPoint(e.Point) && me.pressed[i] {
			go me.selectionListener(i)
			break
		}
	}
	for i, _ := range me.pressed {
		me.pressed[i] = false
	}
}

func (me *FourButtonMenu) Draw(c *gfx.Canvas) {
	for i := 0; i < len(me.text); i++ {
		v := &me.text[i]
		b := me.buttons[i]
		c.PushViewport(b.X, b.Y, b.Width, b.Height)
		if !me.pressed[i] {
			c.SetColor(gfx.Color{255, 255, 255, 170})
			c.FillRoundedRect(10, 10, b.Width-20, b.Height-20, 1)
		}
		var p util.Point
		p.AddTo(b.Size.DivideOf(util.Size{2, 2}).ToPoint())
		p.SubtractFrom(util.Point{v.Width() >> 1, v.Height() >> 1})
		c.DrawText(v, p.X, p.Y)
		c.PopViewport()
	}
}
